<?php

declare(strict_types=1);

namespace app\backend\controller\crm;

use app\common\controller\Basic;
use app\common\service\crm\CustomerService;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use think\facade\Cache;

/**
 * 客户表 控制器
 * @author 吕玉冰
 * @date  2022-07-21
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class Customer extends Basic
{

    // 初始化
    protected function initialize()
    {
        parent::initialize();
        // 当前控制器服务实例化
        $this->service = new CustomerService();
    }

    /**
     * 列表
     */
    public function index()
    {
        if ($this->request->isGet()) {
            $data = input('param.');
            $data['type'] = $this->user['type'];
            $data['user_id'] = $this->user['id'];
            $data['super_id'] = $this->user['super_id'];
            return json($this->service->_list($data));
        }
        return _jerr(1000);
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = input('param.');
            if (!isset($data['allclose'])) {
                $this->_form_filter($data);
                $data['parent_id'] = $this->user['id'];
                $data["create_user_id"] = $this->user['id'];
                $data["create_user_name"] = $this->user['real_name'];
                $data["owner_user_id"] = $this->user['id'];
                $data["owner_user_name"] = $this->user['real_name'];
                return json($this->service->saveForm($data));
            } else {
                return _jok('ok', []);
            }
        }
        if ($this->request->isGet()) {
            $data = [];
            return _jok('ok', $data);
        }
        return _jerr(1000);
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $id = input('id/d', 0);
        if ($id === 0) {
            return _jerr(1022);
        }
        if ($this->request->isPost()) {
            $data = input('param.');
            $data['parent_id'] = $this->user['id'];
            $this->_form_filter($data);

            return json($this->service->saveForm($data));
        }
        if ($this->request->isGet()) {

            $data = $this->service->getInfoById($id);
            return _jok('ok', $data);
        }
        return _jerr(1000);
    }

    /**
     * 添加编辑-表单数据处理
     */
    protected function _form_filter(&$data)
    {
        $data = rq_decode($data); // 前端已 encodeURIComponent 编码（最深二维数组） 后端需要解码
        $data["create_user_id"] = $this->user['id'];
        $data["create_user_name"] = $this->user['real_name'];
        $data["owner_user_id"] = $this->user['id'];
        $data["owner_user_name"] = $this->user['real_name'];
    }

    /**
     * 删除
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = input('id', '');
            if ($id === 0) {
                return _jerr(1002);
            }
            return json($this->service->_del($id));
        }
        return _jerr(1000);
    }

    public function move()
    {
        $id = input('id/d', 0);
        if ($id === 0) {
            return _jerr(1022);
        }
        if ($this->request->isPost()) {
            $data = input('param.');
            $this->_form_filter($data);
            $data['parent_id'] = $data['parent_id'];
            $data['owner_user_id'] = $data['parent_id'];
            $data['owner_user_name'] = $data['parent_id_name'];

            return json($this->service->saveForm($data));
        }
        if ($this->request->isGet()) {
            $data = $this->service->getInfoById($id);
            return _jok('ok', $data);
        }
        return _jerr(1000);
    }

    public function show_address()
    {
        $data = [];
        $data['set'] = "AXGBZ-6VF33-SKG36-3BRXK-3K2QF-CCFOV";
        return _jok('ok', $data);
    }

    public function getAll()
    {
        if ($this->request->isGet()) {
            $data = input('param.');
            $data['type'] = $this->user['type'];
            $data['user_id'] = $this->user['id'];
            $data['super_id'] = $this->user['super_id'];
            return (new CustomerService())->_list($data, false);
        }
        return _jerr(1000);
    }

    public function sendEmail()
    {
        $mail = new PHPMailer(true);
        //Server settings
        //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
        $mail->isSMTP();                                           //Send using SMTP
        $mail->Host = 'smtp.163.com';                     //Set the SMTP server to send through
        $mail->SMTPAuth = true;                                   //Enable SMTP authentication
        $mail->Username = 'lvyubingonly@163.com';                     //SMTP username
        $mail->Password = 'KRQPZHDUHJUSKZPR';                               //SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
        $mail->Port = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

        //Recipients
        $mail->setFrom('lvyubingonly@163.com', 'system');
        $mail->addAddress('354535316@qq.com', '晴天');     //Add a recipient
        $mail->addAddress('lvyubingonly@126.com');               //Name is optional
//      $mail->addReplyTo('354535316@qq.com', 'Information');
//      $mail->addCC('354535316@qq.com');
//      $mail->addBCC('354535316@qq.com');

        //附件
//      $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
//      $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

        //Content
        $mail->isHTML(true);                                  //Set email format to HTML
        $mail->Subject = '通知';
        $mail->Body = '您的客户';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
        $mail->send();
    }

    /**
     * lvyubing
     */
    public function getAllCustomer()
    {
        if ($this->request->isPost()) {
            if (Cache::get("customerList")) {
                $list = Cache::get("customerList");
                return _ok("获取成功1", $list);
            } else {
                $list = (new CustomerService())->getListAll($where = true, ['id' => 'asc'], ["name", "id"]);
                Cache::set("customerList", $list, 300);
                $list = Cache::get("customerList");
                return _ok("获取成功2", $list);
            }
        }
        return _jerr(1000);
    }
}
    